package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.dababases.dao.ItemDao;
import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.GoodsPhoto;
import com.scau.yangsuiyu.estore.dababases.entity.Item;
import com.scau.yangsuiyu.estore.dababases.entity.OrderDetail;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description
 * @date 2019-06-21 16:58
 * @email suiyu_yang@163.com
 */
public class OrderDetailHBox extends HBox implements Initializable {
    private OrderDetail orderDetail;
    private Goods goods;
    private Item item;
    @FXML
    private ImageView goodsPhoto;

    @FXML
    private Label goodsNameLabel;

    @FXML
    private Label promotionalPriceLabel;

    @FXML
    private Label amountLabel;

    @FXML
    private Label sumPriceLabel;


    public OrderDetailHBox(OrderDetail orderDetail) {
        this.orderDetail = orderDetail;
        this.item = new ItemDao().getItemByItemId(orderDetail.getItemId());
        this.goods = item.getGoods();
        loadFXML();

    }


    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/OrderDetailHBox.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        setItemToUI();
    }

    private void setItemToUI() {
        List<GoodsPhoto> goodsPhotos = new ArrayList<>(goods.getGoodsPhotoList());
        ImageViewUtil.loadImage(goodsPhoto, goodsPhotos.get(0).getPhotoUrl());
        goodsNameLabel.setText(goods.getGoodsName());
        promotionalPriceLabel.setText("¥" + goods.getPromotionPrice());
        amountLabel.setText("" + item.getAmount());
        sumPriceLabel.setText("¥" + item.getPaymentPrice());
    }

}
